package com.nxk.modules.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nxk.modules.content.dao.ConNewsDao;
import com.nxk.modules.content.entity.ConNews;
import com.nxk.modules.content.service.ConNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ConNewsServiceImpl extends ServiceImpl<ConNewsDao, ConNews> implements ConNewsService {

    @Override
    @Transactional(readOnly = false)
    public boolean save(ConNews conNews) {
        if (StringUtils.isNotBlank(conNews.getId())) {
            return super.updateById(conNews);
        } else {
            return super.save(conNews);
        }
    }

    @Override
    public IPage<ConNews> page(ConNews conNews) {
        Page<ConNews> page = new Page<ConNews>(conNews.getPageNo(), conNews.getPageSize());
        QueryWrapper<ConNews> queryWrapper = new QueryWrapper<ConNews>();
        if (StringUtils.isNotBlank(conNews.getNewsTitle())) {
            queryWrapper.lambda().like(ConNews::getNewsTitle, conNews.getNewsTitle());
        }
        if (StringUtils.isNotBlank(conNews.getNewsType())){
            queryWrapper.lambda().eq(ConNews::getNewsType,conNews.getNewsType());
        }
        queryWrapper.orderByAsc("sort").in("status", 1, 2);
        IPage<ConNews> result = super.page(page, queryWrapper);
        return result;

    }

    @Override
    @Transactional(readOnly = false)
    public boolean deleteById(String id) {
        return super.removeById(id);
    }
}
